import customRouter from "./customRouter";
import store from '@/store'
import {
	apis
} from './whiteList'
const serverPort = 'https://dms.justom.cn'
const indexOf = (url) => {
	const originUrl = url.split('?');
	return apis.indexOf(originUrl[0])
}
type HttpOptions = {
	[x: string]: any
}
type DownloadOptions = {
	fileName: string
}
type UploadOptions = {
	filePath: string,
	name: string,
	formData: { [x: string]: string }
}

const http = {
	post: (url: string, data: HttpOptions = {}) => {
		return new Promise((resolve, reject) => {
			const header = {
				'Authorization': 'Bearer ' + store.state.userInfo.token
			}
			uni.request({
				url: serverPort + url,
				method: "POST",
				header: header,
				dataType: 'json',
				data: data,
				success: function (res) {
					if (!(res.statusCode == 401 || res.statusCode == 403)) {
						resolve(res.data);
					} else {
						uni.hideLoading()
						if (indexOf(url) == -1) {
							if (res.statusCode == 403) {
								customRouter.reLaunch({
									url: '/pages/tabbar/user/user?popup=true'
								})
							} else {
								customRouter.navigateTo({
									url: '/loginPackages/wxLogin/wxLogin'
								})
							}
						}
						reject(null)
					}
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},
	get: (url: string, data: HttpOptions = {}) => {
		return new Promise((resolve, reject) => {
			const header = {
				'Authorization': 'Bearer ' + store.state.userInfo.token
			}
			uni.request({
				url: serverPort + url,
				dataType: 'json',
				method: 'GET',
				data: data,
				header: header,
				success: function (res) {
					if (!(res.statusCode == 401 || res.statusCode == 403)) {
						resolve(res.data);
					} else {
						uni.hideLoading()
						if (indexOf(url) == -1) {
							if (res.statusCode == 403) {
								customRouter.reLaunch({
									url: '/pages/tabbar/user/user?popup=true'
								})
							} else {
								customRouter.navigateTo({
									url: '/loginPackages/wxLogin/wxLogin'
								})
							}
						}
						reject(null)
					}
				},
				fail: (err) => {
					reject(err)
				}
			})
		})
	},
	downloadFile: (url: string, options: DownloadOptions) => {
		return new Promise((resolve, reject) => {
			const header = {
				'Authorization': 'Bearer ' + store.state.userInfo.token,
				'Content-Type': 'application/octet-stream;charset=UTF-8'
			}
			// #ifdef MP-WEIXIN
			uni.downloadFile({
				url: serverPort + url,
				header,
				filePath: `${uni.env.USER_DATA_PATH}/${options.fileName}`,
				success(res) {
					if (res.statusCode == 200) {
						let path = res.filePath
						const mpPath = res.filePath.split('.')
						path = mpPath[mpPath.length - 1]
						console.log(22);
						uni.openDocument({
							filePath: res.filePath,
							fileType: path,
							success: function (res) {
								resolve(res)
							},
							fail: function (err) {
								uni.hideLoading()
								uni.showToast({
									icon:"error",
									title:'查看失败'
								})
								reject(err);
							}
						});
					}
				},
				fail: function (err) {
					uni.hideLoading()
					uni.showToast({
						icon:"error",
						title:'查看失败'
					})
					reject(err)
				}
			})
			// #endif
			// #ifdef APP-PLUS
			uni.downloadFile({
				url: serverPort + url,
				success(res) {
					let filepathss=plus.io.convertLocalFileSystemURL(res.tempFilePath);
					if (res.statusCode == 200) {
						uni.openDocument({
							filePath: filepathss,
							
							success: function (res) {
								console.log('success');
								resolve(res)
							},
							fail: function (err) {
								uni.hideLoading()
								uni.showToast({
									icon:"error",
									title:'查看失败'
								})
								reject(err);
							}
						});
					}
				},
				fail: function (err) {
					uni.hideLoading()
					uni.showToast({
						icon:"error",
						title:'查看失败'
					})
					reject(err)
				}
			})
			// #endif
			// #ifdef H5
			const el = document.createElement('a');
			el.style.display = 'none'
			el.href = serverPort + url
			document.body.appendChild(el)
			el.click() //点击下载
			resolve(el)
			document.body.removeChild(el) //下载完成移除元素
			// #endif
		})
	},
	uploadFile: (url: string, options: UploadOptions) => {
		return new Promise((resolve, reject) => {
			const header = {
				'Authorization': 'Bearer ' + store.state.userInfo.token
			}
			uni.uploadFile({
				url: serverPort + url,
				header,
				filePath: options.filePath,
				name: options.name,
				formData: options.formData,
				success: function (res) {
					resolve(res.data)
				},
				fail: function (err) {
					reject(err)
				}
			})
		})
	}
}
export {
	serverPort
}
export default http